<template>
  <div class="shopcart-container">

    <div class="goods-list"
         v-if="cart">
      <div class="mui-card">
        <div class="mui-card-content">
          <div class="mui-card-content-inner">
            <mt-switch v-model="$store.getters.getGoodsSelected[cart.id]"
                       @change="selectedChange(cart.id, $store.getters.getGoodsSelected[cart.id])"></mt-switch>
            <img :src="cart.img_url"
                 @click="goGoodsinfo(cart.id)">
            <div class="info">
              <h2>{{cart.title}}</h2>
              <div>
                <span class="price">￥{{cart.new_price}}</span>
                <numbox :initcount="$store.getters.getGoodsCount[cart.id]"
                        :id="cart.id"></numbox>
                <a href="#"
                   @click.prevent="remove(cart.id)">删除</a>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>

    <div class="mui-card">
      <div class="mui-card-content">
        <div class="mui-card-content-inner jiesuan">
          <div class="left">
            <p>总计(不含运费)</p>
            <p>已勾选商品&nbsp;<span>{{$store.getters.getGoodsCountAndAmount.count}}</span>&nbsp;总价&nbsp;￥<span>{{$store.getters.getGoodsCountAndAmount.amount}}</span></p>
          </div>
          <mt-button type="danger">去结算</mt-button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import numbox from '../subcomponents/shopinfo_numbox.vue';

export default {
  data() {
    return {
      cart: null
    }
  },
  created() {
    this.getGoodsList();
  },
  methods: {
    getGoodsList() {
      let cart = this.$store.state.cart[0];
      if (cart) {
        var id = cart.id;
      } else {
        return false;
      }
      this.axios.get('/getshopcarlist/' + id).then(res => {
        this.cart = res.data;
      })
    },
    remove(id) {
      this.cart = null;
      this.$store.commit('removeGoods', id)
    },
    selectedChange(id, val) {
      this.$store.commit('updataGoodsSelecter', { id, selected: val });
    },
    goGoodsinfo(id) {
      this.$router.push('/home/GoodsInfo/' + id);
    }
  },
  components: {
    numbox
  }
}
</script>

<style lang="scss" scoped>
.shopcart-container {
  background-color: #eee;
  overflow: hidden;
  .goods-list {
    .mui-card-content-inner {
      display: flex;
      align-items: center;
    }
    img {
      width: 60px;
      height: 60px;
    }
    h2 {
      font-size: 13px;
    }
    .info {
      display: flex;
      justify-content: space-between;
      flex-direction: column;
      .price {
        color: red;
        font-weight: bold;
      }
    }
  }
  .jiesuan {
    display: flex;
    justify-content: space-between;
    align-items: center;
    .left {
      span {
        color: red;
        font-weight: bold;
        font-size: 16px;
      }
    }
  }
}
</style>